<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>5 原生的R | R语言笔记</title>
  <meta name="description" content="5 原生的R | R语言笔记" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="5 原生的R | R语言笔记" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="5 原生的R | R语言笔记" />
  
  
  

<meta name="author" content="云腾足下" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="统计.html"/>
<link rel="next" href="Eco.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<script src="libs/htmlwidgets-1.5.1/htmlwidgets.js"></script>
<script src="libs/plotly-binding-4.9.2.1/plotly.js"></script>
<script src="libs/typedarray-0.1/typedarray.min.js"></script>
<link href="libs/crosstalk-1.1.0.1/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk-1.1.0.1/js/crosstalk.min.js"></script>
<link href="libs/plotly-htmlwidgets-css-1.52.2/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="libs/plotly-main-1.52.2/plotly-latest.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX","output/SVG"],
  extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  }
});
</script>
<script type="text/javascript"
   src="../../../MathJax/MathJax.js">
</script>


<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>序</a></li>
<li class="chapter" data-level="1" data-path="PanelData.html"><a href="PanelData.html"><i class="fa fa-check"></i><b>1</b> 面板数据</a><ul>
<li class="chapter" data-level="1.1" data-path="PanelData.html"><a href="PanelData.html#一句话tips"><i class="fa fa-check"></i><b>1.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="1.2" data-path="PanelData.html"><a href="PanelData.html#plm包"><i class="fa fa-check"></i><b>1.2</b> <code>plm</code>包</a></li>
<li class="chapter" data-level="1.3" data-path="PanelData.html"><a href="PanelData.html#动态面板阈值估计r语言中有一个包dtp其估计函数为"><i class="fa fa-check"></i><b>1.3</b> 动态面板阈值估计：R语言中有一个包<code>dtp</code>，其估计函数为：</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="TS.html"><a href="TS.html"><i class="fa fa-check"></i><b>2</b> 普通回归和时间序列序列</a><ul>
<li class="chapter" data-level="2.1" data-path="TS.html"><a href="TS.html#一句话tips-1"><i class="fa fa-check"></i><b>2.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="2.2" data-path="TS.html"><a href="TS.html#回归中关于公式的理解和构造"><i class="fa fa-check"></i><b>2.2</b> 回归中关于公式的理解和构造</a></li>
<li class="chapter" data-level="2.3" data-path="TS.html"><a href="TS.html#gmm估计"><i class="fa fa-check"></i><b>2.3</b> GMM估计</a></li>
<li class="chapter" data-level="2.4" data-path="TS.html"><a href="TS.html#季节调整"><i class="fa fa-check"></i><b>2.4</b> 季节调整</a><ul>
<li class="chapter" data-level="2.4.1" data-path="TS.html"><a href="TS.html#r中有x12包可以做季节处理"><i class="fa fa-check"></i><b>2.4.1</b> R中有<code>x12</code>包可以做季节处理</a></li>
<li class="chapter" data-level="2.4.2" data-path="TS.html"><a href="TS.html#seasonal包有x13处理更加便捷"><i class="fa fa-check"></i><b>2.4.2</b> <code>seasonal</code>包有x13处理，更加便捷</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="DataProcess.html"><a href="DataProcess.html"><i class="fa fa-check"></i><b>3</b> 数据处理</a><ul>
<li class="chapter" data-level="3.1" data-path="DataProcess.html"><a href="DataProcess.html#一句话tips-2"><i class="fa fa-check"></i><b>3.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="3.2" data-path="DataProcess.html"><a href="DataProcess.html#rjsdmx包下载世界各大数据库数据"><i class="fa fa-check"></i><b>3.2</b> <code>RJSDMX</code>包下载世界各大数据库数据</a></li>
<li class="chapter" data-level="3.3" data-path="DataProcess.html"><a href="DataProcess.html#前向后向线性和样条插值"><i class="fa fa-check"></i><b>3.3</b> 前向、后向、线性和样条插值</a><ul>
<li class="chapter" data-level="3.3.1" data-path="DataProcess.html"><a href="DataProcess.html#signal包"><i class="fa fa-check"></i><b>3.3.1</b> <code>signal</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="统计.html"><a href="统计.html"><i class="fa fa-check"></i><b>4</b> 统计</a><ul>
<li class="chapter" data-level="4.1" data-path="统计.html"><a href="统计.html#一句话tips-3"><i class="fa fa-check"></i><b>4.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="4.2" data-path="统计.html"><a href="统计.html#mcmc算法"><i class="fa fa-check"></i><b>4.2</b> MCMC算法</a><ul>
<li class="chapter" data-level="4.2.1" data-path="统计.html"><a href="统计.html#吉布斯抽样原理"><i class="fa fa-check"></i><b>4.2.1</b> 吉布斯抽样原理</a></li>
<li class="chapter" data-level="4.2.2" data-path="统计.html"><a href="统计.html#一些共轭先验分布的结论"><i class="fa fa-check"></i><b>4.2.2</b> 一些共轭先验分布的结论</a></li>
<li class="chapter" data-level="4.2.3" data-path="统计.html"><a href="统计.html#一个吉布斯抽样的典型案例"><i class="fa fa-check"></i><b>4.2.3</b> 一个吉布斯抽样的典型案例</a></li>
<li class="chapter" data-level="4.2.4" data-path="统计.html"><a href="统计.html#metropolis-和-m-h算法"><i class="fa fa-check"></i><b>4.2.4</b> Metropolis 和 M-H算法</a></li>
<li class="chapter" data-level="4.2.5" data-path="统计.html"><a href="统计.html#一些带贝叶斯估计的r包使用报告"><i class="fa fa-check"></i><b>4.2.5</b> 一些带贝叶斯估计的R包使用报告</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="rawR.html"><a href="rawR.html"><i class="fa fa-check"></i><b>5</b> 原生的R</a><ul>
<li class="chapter" data-level="5.1" data-path="rawR.html"><a href="rawR.html#一句话tips-4"><i class="fa fa-check"></i><b>5.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="5.2" data-path="rawR.html"><a href="rawR.html#平行计算"><i class="fa fa-check"></i><b>5.2</b> 平行计算</a></li>
<li class="chapter" data-level="5.3" data-path="rawR.html"><a href="rawR.html#类和方法"><i class="fa fa-check"></i><b>5.3</b> 类和方法</a><ul>
<li class="chapter" data-level="5.3.1" data-path="rawR.html"><a href="rawR.html#s3类"><i class="fa fa-check"></i><b>5.3.1</b> S3类</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="rawR.html"><a href="rawR.html#打印到文件"><i class="fa fa-check"></i><b>5.4</b> 打印到文件</a></li>
<li class="chapter" data-level="5.5" data-path="rawR.html"><a href="rawR.html#apply函数族"><i class="fa fa-check"></i><b>5.5</b> <code>apply</code>函数族</a></li>
<li class="chapter" data-level="5.6" data-path="rawR.html"><a href="rawR.html#linux中的r"><i class="fa fa-check"></i><b>5.6</b> Linux中的R</a><ul>
<li class="chapter" data-level="5.6.1" data-path="rawR.html"><a href="rawR.html#如何修改.rprofile"><i class="fa fa-check"></i><b>5.6.1</b> 如何修改<code>.Rprofile</code>？</a></li>
</ul></li>
<li class="chapter" data-level="5.7" data-path="rawR.html"><a href="rawR.html#如何完美复制项目的结果"><i class="fa fa-check"></i><b>5.7</b> 如何完美复制项目的结果</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="Eco.html"><a href="Eco.html"><i class="fa fa-check"></i><b>6</b> 经济学中的各种专业计算</a><ul>
<li class="chapter" data-level="6.1" data-path="Eco.html"><a href="Eco.html#一句话tips-5"><i class="fa fa-check"></i><b>6.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="6.2" data-path="Eco.html"><a href="Eco.html#wwz的贸易增加值分解"><i class="fa fa-check"></i><b>6.2</b> wwz的贸易增加值分解</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="math.html"><a href="math.html"><i class="fa fa-check"></i><b>7</b> 数学计算</a><ul>
<li class="chapter" data-level="7.1" data-path="math.html"><a href="math.html#一句话tips-6"><i class="fa fa-check"></i><b>7.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="7.2" data-path="math.html"><a href="math.html#数值优化"><i class="fa fa-check"></i><b>7.2</b> 数值优化</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="ML.html"><a href="ML.html"><i class="fa fa-check"></i><b>8</b> 机器学习和微观计量</a><ul>
<li class="chapter" data-level="8.1" data-path="ML.html"><a href="ML.html#一句话tips-7"><i class="fa fa-check"></i><b>8.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="8.2" data-path="ML.html"><a href="ML.html#roc曲线绘制"><i class="fa fa-check"></i><b>8.2</b> ROC曲线绘制</a></li>
<li class="chapter" data-level="8.3" data-path="ML.html"><a href="ML.html#randomforestsrc包使用报告"><i class="fa fa-check"></i><b>8.3</b> <code>randomForestSRC</code>包使用报告</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="otherSF.html"><a href="otherSF.html"><i class="fa fa-check"></i><b>9</b> 与其他软件的交互</a><ul>
<li class="chapter" data-level="9.1" data-path="otherSF.html"><a href="otherSF.html#一句话tips-8"><i class="fa fa-check"></i><b>9.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="9.2" data-path="otherSF.html"><a href="otherSF.html#r语言调用matlab"><i class="fa fa-check"></i><b>9.2</b> R语言调用Matlab</a><ul>
<li class="chapter" data-level="9.2.1" data-path="otherSF.html"><a href="otherSF.html#强大的r.matlab包"><i class="fa fa-check"></i><b>9.2.1</b> 强大的<code>R.matlab</code>包</a></li>
<li class="chapter" data-level="9.2.2" data-path="otherSF.html"><a href="otherSF.html#其他的一些小办法"><i class="fa fa-check"></i><b>9.2.2</b> 其他的一些小办法</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="otherSF.html"><a href="otherSF.html#r与python的无缝对接"><i class="fa fa-check"></i><b>9.3</b> R与Python的无缝对接</a><ul>
<li class="chapter" data-level="9.3.1" data-path="otherSF.html"><a href="otherSF.html#其他"><i class="fa fa-check"></i><b>9.3.1</b> 其他：</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="otherSF.html"><a href="otherSF.html#与julia的对接"><i class="fa fa-check"></i><b>9.4</b> 与julia的对接</a><ul>
<li class="chapter" data-level="9.4.1" data-path="otherSF.html"><a href="otherSF.html#juliacall包"><i class="fa fa-check"></i><b>9.4.1</b> <code>JuliaCall</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="DrawTips.html"><a href="DrawTips.html"><i class="fa fa-check"></i><b>10</b> 绘图技巧</a><ul>
<li class="chapter" data-level="10.1" data-path="DrawTips.html"><a href="DrawTips.html#d绘图"><i class="fa fa-check"></i><b>10.1</b> 3D绘图</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html"><i class="fa fa-check"></i><b>11</b> 创建包的一些建议</a><ul>
<li class="chapter" data-level="11.1" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#一句话tips-9"><i class="fa fa-check"></i><b>11.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="11.2" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#使用自己的数据集"><i class="fa fa-check"></i><b>11.2</b> 使用自己的数据集</a></li>
<li class="chapter" data-level="11.3" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#测试"><i class="fa fa-check"></i><b>11.3</b> 测试</a></li>
<li class="chapter" data-level="11.4" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#泛型函数"><i class="fa fa-check"></i><b>11.4</b> 泛型函数</a></li>
<li class="chapter" data-level="11.5" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#其他-1"><i class="fa fa-check"></i><b>11.5</b> 其他</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="其他-2.html"><a href="其他-2.html"><i class="fa fa-check"></i><b>12</b> 其他</a></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">R语言笔记</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="rawR" class="section level1">
<h1><span class="header-section-number">5</span> 原生的R</h1>
<div id="一句话tips-4" class="section level2">
<h2><span class="header-section-number">5.1</span> 一句话Tips</h2>
<ul>
<li>从本地安装一个源码包：</li>
</ul>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="rawR.html#cb1-1"></a><span class="kw">install.packages</span>(<span class="st">&#39;E:</span><span class="ch">\\</span><span class="st">17_HuaDong</span><span class="ch">\\</span><span class="st">NEDatabase</span><span class="ch">\\</span><span class="st">MyRef.Attachments</span><span class="ch">\\</span><span class="st">RForEconometrics/phtt_3.1.2.tar.gz&#39;</span>,<span class="dt">type =</span> <span class="st">&#39;source&#39;</span>,<span class="dt">repos =</span> <span class="ot">NULL</span>)</span></code></pre></div>
<ul>
<li><code>suppressMessages()</code>可以抑制代码抛出的信息等。</li>
<li><code>+(3,1)</code>与<code>3+1</code>的作用是一样的。这可以推广到其他中缀函数，如<code>%*%</code>等。</li>
<li>写函数时，可以使用<code>...</code>参数，为捕获这个(可能是多个)参数的值，可以用<code>list(...)</code>这个办法。</li>
<li>只显示3位小数：</li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="rawR.html#cb2-1"></a><span class="kw">round</span>(<span class="fl">0.123456</span>,<span class="dv">3</span>)</span></code></pre></div>
<pre><code>## [1] 0.123</code></pre>
<ul>
<li>属性赋值：</li>
</ul>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="rawR.html#cb4-1"></a>y &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">5</span>,<span class="dv">8</span>)</span>
<span id="cb4-2"><a href="rawR.html#cb4-2"></a><span class="kw">attr</span>(y,<span class="st">&#39;my_attribute&#39;</span>) &lt;-<span class="st"> &#39;This is a vector&#39;</span></span>
<span id="cb4-3"><a href="rawR.html#cb4-3"></a><span class="kw">attr</span>(y,<span class="st">&#39;my_attribute&#39;</span>)</span></code></pre></div>
<pre><code>## [1] &quot;This is a vector&quot;</code></pre>
<ul>
<li><code>remove.packages('dplyr')</code>，卸载已安装的包。</li>
<li><code>system</code>或<code>shell</code>运行Shell命令。</li>
<li>更新所有的包<code>update.packages(checkBuilt=TRUE, ask=FALSE)</code></li>
<li><code>pkgsearch</code>包的<code>ps</code>函数提供CRAN的关键词搜寻。</li>
<li><code>detach(package:dplyr)</code>可以去掉加载的包。</li>
<li><code>foreach</code>包提供循环的平行计算</li>
<li>在jupyter里面安装R，只需在anaconda里面的命令行中（anaconda prompt）输入，</li>
</ul>
<pre><code>conda install -c r r-essentials</code></pre>
<ul>
<li>工作目录下所有文件名<code>dir()</code></li>
<li><code>file.copy, file.create, file.remove, file.rename, dir.creat, file.exists, file.info</code></li>
<li><code>file.rename</code>批量修改文件名</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="rawR.html#cb7-1"></a>fr =<span class="st"> </span><span class="kw">paste</span>(<span class="st">&#39;./加工贸易HS/2016/&#39;</span>,<span class="kw">dir</span>(<span class="st">&#39;./加工贸易HS/2016&#39;</span>),<span class="dt">sep =</span> <span class="st">&#39;&#39;</span>)</span>
<span id="cb7-2"><a href="rawR.html#cb7-2"></a>to =<span class="st"> </span><span class="kw">paste</span>(<span class="st">&#39;./加工贸易HS/2016/hp&#39;</span>,<span class="kw">dir</span>(<span class="st">&#39;./加工贸易HS/2016&#39;</span>),<span class="dt">sep =</span> <span class="st">&#39;&#39;</span>)</span>
<span id="cb7-3"><a href="rawR.html#cb7-3"></a><span class="kw">file.rename</span>(<span class="dt">from =</span> fr,<span class="dt">to =</span> to)</span></code></pre></div>
<ul>
<li><code>down.file</code>只要给出第一个参数：网址（包括ftp的）和第二个参数，下载的文件要保存的文件名，就可以直接在网上下载文件。如果中国乱码，记得使用fileEncoding = ’UTF-8’来修正。</li>
<li><code>getAnywhere(predict.Arima)</code>查看源代码</li>
<li><code>.rs.restartR()</code>重启一个新的R会话</li>
<li>如何安装已经过期的包？
<ol style="list-style-type: decimal">
<li>点<a href="https://cran.r-project.org/src/contrib/Archive/">这里</a>找到过期的包，然后下载下来。</li>
<li>用这个命令安装本地的包：<code>install.packages('D:/MSBVAR_0.9-3.tar.gz',repos = NULL, type = 'source')</code></li>
</ol></li>
</ul>
</div>
<div id="平行计算" class="section level2">
<h2><span class="header-section-number">5.2</span> 平行计算</h2>
<p>平行计算中，光使用<code>foreach</code>包是不够的，还需要注册一个平行背景注册，否则<code>foreach</code>包在运算完以后会返回警告：</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="rawR.html#cb8-1"></a><span class="co"># Warning message:</span></span>
<span id="cb8-2"><a href="rawR.html#cb8-2"></a><span class="co">#</span></span>
<span id="cb8-3"><a href="rawR.html#cb8-3"></a><span class="co"># executing %dopar% sequentially: no parallel backend registered </span></span></code></pre></div>
<p>平行计算一般的工作流如下：</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="rawR.html#cb9-1"></a><span class="kw">library</span>(doParallel)</span>
<span id="cb9-2"><a href="rawR.html#cb9-2"></a>cl &lt;-<span class="st"> </span>parallel<span class="op">::</span><span class="kw">makeCluster</span>(<span class="dv">2</span>)</span>
<span id="cb9-3"><a href="rawR.html#cb9-3"></a>doParallel<span class="op">::</span><span class="kw">registerDoParallel</span>(cl)</span>
<span id="cb9-4"><a href="rawR.html#cb9-4"></a><span class="kw">foreach</span>(<span class="dt">i=</span><span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="dt">.pacakages =</span> <span class="st">&#39;tidyverse&#39;</span>) <span class="op">%dopar%</span><span class="st"> </span><span class="kw">sqrt</span>(i)</span>
<span id="cb9-5"><a href="rawR.html#cb9-5"></a>parallel<span class="op">::</span><span class="kw">stopCluster</span>(cl)</span></code></pre></div>
<p>要注意，平行计算中，在<code>foreach</code>后的语句中，相当于在每个进程中，重启了一个新环境。因此，如果你要用到<code>foreach</code>外面的变量，则需要把变量、包等都传进去。同时，这些变量如果是向量或者<code>list</code>则不需要特别地指定迭代变量是哪个，程序会自动将它们处理成迭代变量。如果这些变量的长度不一，则迭代时以最少长度的变量为准。一个简单的例子(<code>VARrf::IRFrf_gen</code>)如下：</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="rawR.html#cb10-1"></a><span class="co"># 传进去了5个变量,nhist, itevar,pmax,s, shockvar，这些变量的长度都是一样的。</span></span>
<span id="cb10-2"><a href="rawR.html#cb10-2"></a>picdata &lt;-<span class="st"> </span>foreach<span class="op">::</span><span class="kw">foreach</span>(<span class="dt">i =</span> <span class="dv">1</span><span class="op">:</span>nhist,<span class="dt">itevar =</span> itevar,</span>
<span id="cb10-3"><a href="rawR.html#cb10-3"></a>                              <span class="dt">pmax =</span> pmax_para,<span class="dt">s =</span> s, <span class="dt">shockvar =</span> shockvar,</span>
<span id="cb10-4"><a href="rawR.html#cb10-4"></a>                              <span class="dt">.packages =</span> <span class="st">&#39;tidyverse&#39;</span>) <span class="op">%dopar%</span><span class="st"> </span>{</span>
<span id="cb10-5"><a href="rawR.html#cb10-5"></a>    devtools<span class="op">::</span><span class="kw">load_all</span>()</span>
<span id="cb10-6"><a href="rawR.html#cb10-6"></a>    <span class="kw">IRFrf</span>(<span class="dt">data =</span> itevar, <span class="dt">pmax =</span> pmax, <span class="dt">s =</span> s, <span class="dt">shockvar =</span> shockvar,<span class="dt">histime =</span> i)</span>
<span id="cb10-7"><a href="rawR.html#cb10-7"></a>  }</span></code></pre></div>
</div>
<div id="类和方法" class="section level2">
<h2><span class="header-section-number">5.3</span> 类和方法</h2>
<div id="s3类" class="section level3">
<h3><span class="header-section-number">5.3.1</span> S3类</h3>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="rawR.html#cb11-1"></a><span class="co"># 查看属于一个泛型函数的所有方法：</span></span>
<span id="cb11-2"><a href="rawR.html#cb11-2"></a><span class="kw">methods</span>(<span class="st">&#39;mean&#39;</span>)</span></code></pre></div>
<pre><code>## [1] mean.Date     mean.default  mean.difftime mean.POSIXct  mean.POSIXlt 
## [6] mean.quosure*
## see &#39;?methods&#39; for accessing help and source code</code></pre>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="rawR.html#cb13-1"></a><span class="co"># 反过来，查看一个类，都有何关联的泛型函数</span></span>
<span id="cb13-2"><a href="rawR.html#cb13-2"></a><span class="kw">methods</span>(<span class="dt">class =</span> <span class="st">&#39;ts&#39;</span>)</span></code></pre></div>
<pre><code>##  [1] [             [&lt;-           aggregate     as.data.frame cbind        
##  [6] coerce        cycle         diff          diffinv       initialize   
## [11] kernapply     lines         Math          Math2         monthplot    
## [16] na.omit       Ops           plot          print         show         
## [21] slotsFromS3   t             time          window        window&lt;-     
## see &#39;?methods&#39; for accessing help and source code</code></pre>
<p>创建一个类，很多时候只需在最后返回一个这样的，就可以了，</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="rawR.html#cb15-1"></a><span class="kw">class</span>(foo) &lt;-<span class="st"> &#39;myclass&#39;</span>   </span></code></pre></div>
<p>然后为这个类创建一个泛型函数，只需要两步：</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="rawR.html#cb16-1"></a><span class="co"># 创建一个类</span></span>
<span id="cb16-2"><a href="rawR.html#cb16-2"></a>a &lt;-<span class="st"> </span><span class="kw">list</span>()</span>
<span id="cb16-3"><a href="rawR.html#cb16-3"></a><span class="kw">class</span>(a) &lt;-<span class="st"> &#39;a&#39;</span></span>
<span id="cb16-4"><a href="rawR.html#cb16-4"></a><span class="co"># 第一步：增加一个新的泛型函数。记住，没有搭配该泛型函数的方法，泛型函数是没有用的。</span></span>
<span id="cb16-5"><a href="rawR.html#cb16-5"></a>f &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="kw">UseMethod</span>(<span class="st">&#39;f&#39;</span>) </span>
<span id="cb16-6"><a href="rawR.html#cb16-6"></a><span class="co"># 第二步，为此泛型函数添加方法。关键在于命名规则，属于该泛型函数的方法一定具有类似f.a格式的命名。</span></span>
<span id="cb16-7"><a href="rawR.html#cb16-7"></a><span class="co"># f是泛型函数, a是类,它们用点连起来。</span></span>
<span id="cb16-8"><a href="rawR.html#cb16-8"></a>f.a &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="st">&#39;class a&#39;</span></span>
<span id="cb16-9"><a href="rawR.html#cb16-9"></a>mean.a &lt;-<span class="st"> </span><span class="cf">function</span>(x) <span class="st">&#39;a&#39;</span> <span class="co"># 为已有的泛型函数增加方法</span></span></code></pre></div>
</div>
</div>
<div id="打印到文件" class="section level2">
<h2><span class="header-section-number">5.4</span> 打印到文件</h2>
<ul>
<li><code>sink</code>函数：在代码开始前加一行：<code>sink(“output.txt”)</code>，就会自动把结果全部输出到工作文件夹下的output.txt文本文档。这时在R控制台的输出窗口中是看不到输出结果的。代码结束时用<code>sink()</code>切换回来。 示例：</li>
</ul>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="rawR.html#cb17-1"></a><span class="kw">sink</span>(<span class="st">&quot;a.txt&quot;</span>) </span>
<span id="cb17-2"><a href="rawR.html#cb17-2"></a>x&lt;-<span class="kw">rnorm</span>(<span class="dv">100</span>,<span class="dv">0</span>,<span class="dv">1</span>) </span>
<span id="cb17-3"><a href="rawR.html#cb17-3"></a><span class="kw">mean</span>(x) </span>
<span id="cb17-4"><a href="rawR.html#cb17-4"></a><span class="kw">sink</span>()</span></code></pre></div>
<ul>
<li><code>cat</code>函数：<code>cat('abc','OK!',file = 'a.txt',sep = '\n',append = T)</code></li>
<li><code>stargazer</code>函数：</li>
</ul>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="rawR.html#cb18-1"></a><span class="kw">stargazer</span>(fit1, fit2, <span class="dt">title =</span> <span class="st">&quot;results&quot;</span>, <span class="dt">align =</span> F, <span class="dt">type =</span> <span class="st">&quot;text&quot;</span>, <span class="dt">no.space =</span> <span class="ot">TRUE</span>, <span class="dt">out =</span> <span class="st">&quot;fit.html&quot;</span>)</span></code></pre></div>
</div>
<div id="apply函数族" class="section level2">
<h2><span class="header-section-number">5.5</span> <code>apply</code>函数族</h2>
<ul>
<li><code>lapply(vector,fun)</code>，可以将函数作用于向量<code>vector</code>的每一个元素上，然后返回一个<code>list</code>。</li>
<li><code>sapply(vector,fun)</code>的好处在于不是返回一个list，而是返回一个向量或者矩阵。如果<code>fun</code>返回的是一个元素，那么<code>sapply</code>就返回一个向量，如果<code>fun</code>返回的是一个向量，则<code>sapply</code>按列将结果拼接成一个矩阵。</li>
</ul>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="rawR.html#cb19-1"></a><span class="kw">sapply</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>, <span class="cf">function</span>(i) i<span class="op">^</span><span class="dv">2</span>)</span></code></pre></div>
<pre><code>##  [1]   1   4   9  16  25  36  49  64  81 100</code></pre>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="rawR.html#cb21-1"></a><span class="kw">sapply</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>, <span class="cf">function</span>(i) <span class="kw">c</span>(i<span class="op">^</span><span class="dv">2</span>,i))</span></code></pre></div>
<pre><code>##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    1    4    9   16   25   36   49   64   81   100
## [2,]    1    2    3    4    5    6    7    8    9    10</code></pre>
<ul>
<li><code>apply</code>是我用得最熟的，它是将函数应用到矩阵或数组的行或列上。</li>
<li><code>mapply</code>是<code>lapply</code>或<code>sapply</code>的多元版本，即它可以同时输入多个向量，如</li>
</ul>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="rawR.html#cb23-1"></a><span class="kw">mapply</span>(<span class="cf">function</span>(x,y,z) x<span class="op">*</span>y <span class="op">+</span><span class="st"> </span>y<span class="op">*</span>z <span class="op">+</span><span class="st"> </span>x<span class="op">*</span>z, <span class="dt">x =</span> <span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>),<span class="dt">y =</span> <span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>), <span class="dt">z =</span> <span class="kw">c</span>(<span class="op">-</span><span class="dv">1</span>,<span class="op">-</span><span class="dv">2</span>,<span class="op">-</span><span class="dv">3</span>))</span></code></pre></div>
<pre><code>## [1] -1 -4 -9</code></pre>
</div>
<div id="linux中的r" class="section level2">
<h2><span class="header-section-number">5.6</span> Linux中的R</h2>
<p>很多时候，对服务器我们没有权限，因此我们只能下载包的源码，然后安装。工作流如下：
- 在自己有权限的目录下新建一个目录，比如<code>/data/stage/chenpu/RLib</code>作为包的安装目录，命令为</p>
<pre><code>mkdir /data/stage/chenpu/RLib</code></pre>
<ul>
<li>将包的源码下载下来，然后命令行安装</li>
</ul>
<pre><code>R CMD INSTALL /.../mypackage.tar.gz --library=/data/stage/chenpu/RLib</code></pre>
<ul>
<li>在R跑程序，加载的时候，要注意<code>lib.loc</code>参数，</li>
</ul>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="rawR.html#cb27-1"></a><span class="kw">library</span>(zoo, <span class="dt">lib.loc =</span> <span class="st">&#39;/data/stage/chenpu/RLib&#39;</span>)</span></code></pre></div>
<p>或者修改启动文件<code>.Rprofile</code>，这就不会每次启动R都要重新设置了。</p>
<div id="如何修改.rprofile" class="section level3">
<h3><span class="header-section-number">5.6.1</span> 如何修改<code>.Rprofile</code>？</h3>
<p>进入R以后，</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="rawR.html#cb28-1"></a><span class="kw">R.home</span>() <span class="co"># 确定R安装在了哪里</span></span>
<span id="cb28-2"><a href="rawR.html#cb28-2"></a><span class="kw">file.edit</span>(<span class="kw">file.path</span>(<span class="st">&#39;~&#39;</span>,<span class="st">&#39;.Rprofile&#39;</span>)) <span class="co"># 编辑Rprofile</span></span>
<span id="cb28-3"><a href="rawR.html#cb28-3"></a><span class="co"># 此时进入vi界面， 在插入模式下，键入</span></span>
<span id="cb28-4"><a href="rawR.html#cb28-4"></a><span class="kw">.libPaths</span>(<span class="st">&#39;/data/stage/chenpu/RLib&#39;</span>)</span>
<span id="cb28-5"><a href="rawR.html#cb28-5"></a><span class="co"># 再按Esc退出插入模式进入命令行模式，输入`:wq`保存退出。</span></span></code></pre></div>
</div>
</div>
<div id="如何完美复制项目的结果" class="section level2">
<h2><span class="header-section-number">5.7</span> 如何完美复制项目的结果</h2>
<p>在写一个项目时，往往对应着当时你调用的各种R包的版本。一年以后，许多包已经更新，可能你当时的代码结果已经无法复制。我们需要管理不同版本的R包。</p>
<p><code>renv</code>包就是干这个的。工作流如下：</p>
<ol style="list-style-type: decimal">
<li><code>renv::init()</code>初始化这个项目，会生产对应该项目的包库文件目录。此时安装包，都是在这个私有库下进行，不影响全局的包库。</li>
<li><code>renv::snapshot()</code>记下此时包的各个版本。</li>
<li><code>renv::restore()</code>恢复上一个快照时包库的状态。</li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="统计.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="Eco.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["CBook.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
